﻿using BasicComponents.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BasicComponents.dialogs
{
    /// <summary>
    /// Interaction logic for Dialog_TreeDMMC.xaml
    /// </summary>
    public partial class Dialog_ListBox : Window
    {
        ObservableCollection<ListBoxItemResourses> list_res = new ObservableCollection<ListBoxItemResourses>();
        ObservableCollection<ListBoxItemResourses> list_listboxItems = new ObservableCollection<ListBoxItemResourses>();
        ObservableCollection<ListBoxItemResourses> list_SelectedItems = new ObservableCollection<ListBoxItemResourses>();
        DataTable dtRAW = null;
        String dm = "";
        String mc = "";
        String fjfa;
        char spliterChar;
        String dmColName;
        String mcColName;

        public string Dm
        {
            get
            {
                return dm;
            }

            set
            {
                dm = value;
            }
        }

        public string Mc
        {
            get
            {
                return mc;
            }

            set
            {
                mc = value;
            }
        }

        public ObservableCollection<ListBoxItemResourses> List_listboxItems
        {
            get
            {
                return list_listboxItems;
            }

            set
            {
                list_listboxItems = value;
            }
        }

        public ObservableCollection<ListBoxItemResourses> List_SelectedItems
        {
            get
            {
                return list_SelectedItems;
            }

            set
            {
                list_SelectedItems = value;
            }
        }

        public Dialog_ListBox()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        //初始化数据
        public void iniData(ObservableCollection<ListBoxItemResourses> listData)
        {
            this.list_res.Clear();
            this.list_listboxItems = listData;
            foreach (ListBoxItemResourses lbir in listData)
            {
                this.list_res.Add(lbir);
            }
        }
        //刷新
        private void refresh(String fjfa, char spliterChar, DataTable dt, String dmColName, String mcColName)
        {
            //this.treeView.Items.Clear();
            //foreach (DataRow row in dt.Rows)
            //{
            //    String dm = row[dmColName].ToString();
            //    String preDM = DMFJUtils.DMFJUtils.pearentDM(dm, spliterChar, fjfa);
            //    TreeViewItem tvi_new = new TreeViewItem();
            //    tvi_new.Name = "_" + row[dmColName].ToString();
            //    tvi_new.Header = row[dmColName] + "-" + row[mcColName];
            //    tvi_new.MouseDoubleClick += Tvi_new_MouseDoubleClick;
            //    if (preDM == null || preDM.Trim().Equals(""))
            //    {
            //        this.treeView.Items.Add(tvi_new);
            //    }
            //    else
            //    {
            //        TreeViewItem preNode = null;
            //        foreach (TreeViewItem tvi_root in this.treeView.Items)
            //        {
            //            preNode = getPreTreeNode(tvi_root, preDM);
            //            if (preNode != null)
            //            {
            //                preNode.Items.Add(tvi_new);
            //                break;
            //            }
            //        }
            //        if (preNode == null)
            //        {
            //            this.treeView.Items.Add(tvi_new);
            //        }
            //    }
            //}
        }
        //双击节点事件
        private void Tvi_new_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!(sender as TreeViewItem).HasItems)
            {
                btn_selected_Click(this.btn_selected, null);
            }
        }

        //遍历节点信息
        private TreeViewItem getPreTreeNode(TreeViewItem tvi_root, String preDM)
        {
            if (tvi_root.Name.Substring(1).Equals(preDM))
            {
                return tvi_root;
            }
            else if (preDM.IndexOf(tvi_root.Name.Substring(1)) == 0 && tvi_root.HasItems)
            {
                foreach (TreeViewItem tbi in tvi_root.Items)
                {
                    TreeViewItem tvi = getPreTreeNode(tbi, preDM);
                    if (tvi != null)
                    {
                        return tvi;
                    }
                    else
                    {
                        continue;
                    }
                }
                return null;
            }
            else
            {
                return null;
            }
        }
        //搜索按钮点击事件
        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String keyWords = this.uc_keywords.TxtValue; 
                this.list_listboxItems.Clear();
                foreach (ListBoxItemResourses lbir in this.list_res)
                {
                    if (lbir.Dmmc.IndexOf(keyWords) >= 0 || NPinyin.Pinyin.GetInitials(lbir.Dmmc).ToUpper().IndexOf(keyWords.ToUpper()) >= 0)
                    {
                        this.list_listboxItems.Add(lbir);
                    }
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show("筛选失败，错误信息：" + e1.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            } 
        }
        //取消筛选按钮事件
        private void btn_CancelSearch_Click(object sender, RoutedEventArgs e)
        {
            this.uc_keywords.TxtValue = "";
            this.list_listboxItems.Clear();
            foreach (ListBoxItemResourses lbir in this.list_res)
            {
                this.list_listboxItems.Add(lbir);
            }
        }
        //选择按钮点击事件
        private void btn_selected_Click(object sender, RoutedEventArgs e)
        {
            this.List_SelectedItems.Clear();
            if (this.listBox.SelectedItems.Count > 0)
            {
                foreach (ListBoxItemResourses item in this.listBox.SelectedItems)
                {
                    this.List_SelectedItems.Add(item.Clone() as ListBoxItemResourses);
                }
                this.DialogResult = true;
            }
            else return;
        }
        //取消按钮事件
        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.List_SelectedItems.Clear();
            this.DialogResult = false;
        }

        private void btn_selectAll_Click(object sender, RoutedEventArgs e)
        {
            this.listBox.SelectAll();
        }

        private void btn_unSelectAll_Click(object sender, RoutedEventArgs e)
        {
            this.listBox.UnselectAll();
        }
    }
}
